#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 100
typedef int ELemType;
typedef struct {
  ELemType data[MAXSIZE];
  int front;
  int rear;
}Queue;

void initQueue(Queue *Q){
  Q->front=0;
  Q->rear=0;
}

int isEmpty(Queue *Q){
  if(Q->front==Q->rear){
    printf("队列为空\n");
    return 1;
  }
  return 0;
}

//出队
ELemType dequeue(Queue *Q){
  if(isEmpty(Q)==1){
    return 0;
  }
  ELemType e=Q->data[Q->front];
  Q->front++;
  return e;
}

//调整队列，腾出空位
int queueFull(Queue *Q){
  if(Q->front>0){
    int step=Q->front;
    for (int i = Q->front; i<=Q->rear; i++){
      Q->data[i-step]=Q->data[i];
    }
    Q->front=0;
    Q->rear=Q->rear-step;
    return 1;
  }else{
    printf("队列已满\n");
    return 0;
  }
}

//入队
int equeue(Queue *Q,ELemType e){
  if(Q->rear>=MAXSIZE){
    if(!queueFull(Q)){
      return 0;
    }
  }
  Q->data[Q->rear]=e;
  Q->rear++;
  return 1;
}

//获取队头
int getHead(Queue *Q,ELemType *e){
  if(isEmpty(Q)==1) return 0;
  *e=Q->data[Q->front];
  return 1;
}

int main(){
  Queue q;
	initQueue(&q);

	equeue(&q, 10);
	equeue(&q, 20);
	equeue(&q, 30);
	equeue(&q, 40);
	equeue(&q, 50);
	
	printf("%d\n",dequeue(&q));
	printf("%d\n",dequeue(&q));
	ELemType e;
	getHead(&q, &e);
	printf("%d\n",e);

	return 0;
}

